import {defineStore} from "pinia/dist/pinia";
import {selfMenus as remoteSelfMenus} from "@/api/sys/menu";
import {dynamicRouter} from "@/router";

export const useMenuStore = defineStore('menuStore', {
  state: () => {
    return {
      menus: []
    }
  },
  getters: {
    navigationData(state) {
      return state.menus
    },
    initialized(state) {
      return state.menus.length > 0
    }
  },
  actions: {
    async init(router) {
      const {code, data} = await remoteSelfMenus()
      if (code === 0) {
        // 初始化菜单
        this.menus = data
        // 递归新增路由
        await dynamicRouter(router, data)
      }
    }
  }
})
